package com.zy.www.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.DetectFaceRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 人脸属性检测代码示例
 */
public class DetectFaceSample {

    public static void main(String[] args) throws Exception {
        //请替换成您自己的accessKeyId、accessKeySecret
        IClientProfile profile = DefaultProfile
                .getProfile("cn-shanghai", "您的accessKeyId", "您的accessKeySecret");
        DefaultProfile
                .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        DetectFaceRequest detectFaceRequest = new DetectFaceRequest();
        detectFaceRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
        detectFaceRequest.setMethod(MethodType.POST); // 指定请求方法
        detectFaceRequest.setEncoding("utf-8");
        detectFaceRequest.setHttpContentType(FormatType.JSON);

        JSONObject data = new JSONObject();
        // 设置待检测的人脸图片
        data.put("url", "http://xxxx.xx/xx");
        detectFaceRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请设置超时时间, 服务端全链路处理超时时间为10秒，请做相应设置
         * 如果您设置的ReadTimeout小于服务端处理的时间，程序中会获得一个read timeout异常
         */
        detectFaceRequest.setConnectTimeout(3000);
        detectFaceRequest.setReadTimeout(10000);

        HttpResponse httpResponse;
        try {
            httpResponse = client.doAction(detectFaceRequest);
        } catch (Exception e) {
            e.printStackTrace();
            // 异常处理
            return;
        }

        // 判断HTTP请求是否成功
        if (httpResponse.isSuccess()) {
            JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
            if (200 == scrResponse.getInteger("code")) {
                JSONObject result = scrResponse.getJSONObject("data");
                if (200 == result.getInteger("code")) {
                    /**
                     *   结果数据
                     */
                    JSONArray faces = result.getJSONArray("faces");
                    // 检测到的人脸个数
                    System.out.println("faceCount:" + faces.size());
                    // 每个人脸的结果详情
                    for (int i = 0; i < faces.size(); i++) {
                        System.out.println("face" + i + ": " + faces.getJSONObject(i).toJSONString());
                    }
                } else {
                    System.out.println("result fail: " + result.toJSONString());
                }
            } else {
                /**
                 * 请求整体处理失败，原因视具体的情况详细分析
                 */
                System.out.println("request fail:" + scrResponse.toJSONString());
            }
        } else {
            /**
             * HTTP请求失败，错误详情
             */
            System.out.println("response fail!" +
                    "\ncode:" + httpResponse.getStatus() +
                    "\nmsg:" + new String(httpResponse.getHttpContent(), "UTF-8"));
        }

    }
}